package com.insleton.admin.image.action;

import jakarta.inject.Inject;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;

import com.google.common.collect.Lists;
import com.vteba.utils.math.NumberUtils;
import com.vteba.utils.common.MediaTypes;
import com.vteba.utils.id.ObjectId;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.insleton.admin.image.model.ImageResource;
import com.insleton.admin.image.model.ImageResourceReply;
import com.insleton.admin.image.service.spi.ImageResourceService;

import com.vteba.web.action.GenericAction;

/**
 * 图片资源控制器
 *
 * @author yinlei
 * @since 2025年6月17日 10:30:55
 */
@Tag(name = "图片资源控制器")
@Controller
@RequestMapping(value = "/imageResource", produces = MediaTypes.PROTOBUF, consumes = MediaTypes.PROTOBUF)
public class ImageResourceAction extends GenericAction<ImageResource> {
	private static final Logger LOGGER = LogManager.getLogger(ImageResourceAction.class);

	@Inject
	private ImageResourceService imageResourceService;

	/**
	 * 查询图片资源list
	 *
	 * @param model 参数
	 * @return 图片资源List
	 */
	@Operation(summary = "查询图片资源列表", operationId = "查询图片资源列表")
	@ResponseBody
	@PostMapping("/list")
	public ImageResourceReply list(@RequestBody ImageResource model) {
		ImageResourceReply reply = new ImageResourceReply();
		try {
			// String tenantId = model.getTenantId();
			// if (StringUtils.isAnyBlank(tenantId)) {
			//     reply.setCode(3);
			//     reply.setMessage("机构参数不能为空。");
			//     return reply;
			// }
			List<ImageResource> imageResourceList = imageResourceService.queryList(model);
			if (CollectionUtils.isNotEmpty(imageResourceList)) {
				reply.setCode(1);
				reply.setData(imageResourceList);
			} else {
				reply.setCode(2);
				reply.setMessage("没有获取到图片资源数据。");
			}
		} catch (Exception e) {
			LOGGER.error("查询图片资源list错误。", e);
			reply.setCode(-1);
			reply.setMessage("查询图片资源列表错误");
		}
		return reply;
	}

	// /**
	//  * 查询图片资源，更通用的查询。
	//  *
	//  * @param imageResource 参数
	//  * @return 图片资源结果
	//  */
	// @Operation(summary = "更通用的查询图片资源", operationId = "更通用的查询图片资源")
	// @ResponseBody
	// @PostMapping("/query")
	// public ImageResourceReply query(@RequestBody ImageResource imageResource) {
	// 	ImageResourceReply reply = new ImageResourceReply();
	// 	try {
	// 	    // String tenantId = imageResource.getTenantId();
	// 	    // if (StringUtils.isAnyBlank(tenantId)) {
	// 	    //     reply.setCode(3);
	// 	    //     reply.setMessage("机构参数不能为空。");
	// 	    //     return reply;
	// 	    // }
	// 		List<ImageResource> list = imageResourceService.queryList(imageResource);
	// 		if (CollectionUtils.isNotEmpty(list)) {
	// 			reply.setCode(1);
	// 			reply.setData(list);
	// 		} else {
	// 			reply.setCode(2);
	// 			reply.setMessage("没有获取到图片资源数据。");
	// 		}
	// 	} catch (Exception e) {
	// 		LOGGER.error("imageResource query error, msg=[{}]", e.getMessage(), e);
	// 		reply.setCode(-1);
	// 		reply.setMessage("查询图片资源错误。");
	// 	}
	// 	return reply;
	// }

	/**
	 * 新增图片资源
	 *
	 * @param model 要新增的数据
	 * @return 操作结果
	 */
	@Operation(summary = "新增图片资源", operationId = "新增图片资源")
	@ResponseBody
	@PostMapping("/add")
	public ImageResourceReply add(@RequestBody ImageResource model) {
		ImageResourceReply reply = new ImageResourceReply();
		try {
			model.setId(ObjectId.get().toString());
			int result = imageResourceService.save(model);
			if (result == 1) {
				reply.setMessage(SUCCESS);
				reply.setCode(1);
			} else {
				reply.setMessage(ERROR);
				reply.setCode(2);
				LOGGER.error("save imageResource error.");
			}
		} catch (Exception e) {
			LOGGER.error("save imageResource error, msg=[{}].", e.getMessage(), e);
			reply.setMessage(ERROR);
			reply.setCode(-1);
		}
		return reply;
	}

	/**
	 * 查看图片资源详情。
	 *
	 * @param model 查询参数，携带ID
	 * @return 图片资源详情
	 */
	@Operation(summary = "查看图片资源详情", operationId = "查看图片资源详情")
	@ResponseBody
	@PostMapping("/detail")
	public ImageResourceReply detail(@RequestBody ImageResource model) {
		ImageResourceReply reply = new ImageResourceReply();
		try {
			String id = model.getId();
			if (StringUtils.isBlank(id)) {
				reply.setCode(2);
				reply.setMessage("params's id cannot be null.");
				return reply;
			}

			model = imageResourceService.get(id);

			reply.setData(Lists.newArrayList(model));
			reply.setCode(1);
		} catch (Exception e) {
			LOGGER.error("query imageResource detail, msg=[{}].", e.getMessage(), e);
			reply.setCode(-1);
			reply.setMessage("查询图片资源详情错误。");
		}
		return reply;
	}

	/**
	 * 更新图片资源信息
	 *
	 * @param model 要更新的图片资源信息，含有ID
	 * @return 操作结果
	 */
	@Operation(summary = "更新图片资源信息", operationId = "更新图片资源信息")
	@ResponseBody
	@PostMapping("/update")
	public ImageResourceReply update(@RequestBody ImageResource model) {
		ImageResourceReply reply = new ImageResourceReply();
		try {
			// TODO 记得验证参数，联合主键没有验证主键非空
			String id = model.getId();
			if (StringUtils.isBlank(id)) {
				reply.setCode(2);
				reply.setMessage("params's id cannot be null.");
				return reply;
			}
			int result = imageResourceService.updateById(model);
			if (result == 1) {
				reply.setMessage(SUCCESS);
				reply.setCode(1);
			} else {
				reply.setMessage("更新图片资源错误。");
				reply.setCode(3);
				LOGGER.error("update imageResource error imageResourceId=[{}].", id);
			}
		} catch (Exception e) {
			LOGGER.error("update imageResource error, msg=[{}].", e.getMessage(), e);
			reply.setMessage(ERROR);
			reply.setCode(-1);
		}
		return reply;
	}

	/**
	 * 删除图片资源信息
	 *
	 * @param model 要删除的图片资源，携带id参数
	 */
	@Operation(summary = "删除图片资源信息", operationId = "删除图片资源信息")
	@ResponseBody
	@PostMapping("/delete")
	public ImageResourceReply delete(@RequestBody ImageResource model) {
		ImageResourceReply reply = new ImageResourceReply();
		try {
			String id = model.getId();
			if (StringUtils.isBlank(id)) {
				reply.setCode(2);
				reply.setMessage("params's id cannot be null.");
				return reply;
			}

			int result = imageResourceService.deleteById(id);
			if (result == 1) {
				reply.setMessage(SUCCESS);
				reply.setCode(1);
			} else {
				reply.setMessage("删除图片资源错误。");
				reply.setCode(3);
				LOGGER.error("delete imageResource error imageResourceId=[{}].", id);
			}
		} catch (Exception e) {
			LOGGER.error("delete imageResource error, msg=[{}].", e.getMessage());
			reply.setMessage(ERROR);
			reply.setCode(-1);
		}
		return reply;
	}
}
